<?php

/**
 * 自动生成 AttachmentsService
 * 
 * @filepath  Apps/Common/Service/AttachmentsService.class.php
 * @author  Zix 
 * @version v1.0 , 2015-09-13
 */

namespace Common\Service;

class AttachmentsService {

  private static $instance;
  var $owner = array(
    101 => '微信活动-投票' ,
  );
  
  const WX_EVENT_VOTE = 101 ;
  
  var $is_image = array(
    0 => '否' ,
    1 => '是' ,
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new AttachmentsService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'owner' => '1',
      'owner_id' => '',
      'savepath' => '',
      'size' => '',
      'mimes' => '',
      'is_image' => '1',
      'image_info' => '',
      'width' => '',
      'height' => '',
      'create_time' => date('Y-m-d H:i:s'),
    );
  }

  function get_by_id($id) {
    $Attachments = M('Attachments');
    $data = $Attachments->find($id);
    return $data ? $data : array();
  }

  function get_by_cond( $config ) {
    $default = array(
      'owner' => '',
      'owner_id' => '',
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $Attachments = M('Attachments');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }
    
    if (!empty($config['owner'])) {
      $where['owner'] = $config['owner'];
    }

    if (!empty($config['owner_id'])) {
      $where['owner_id'] = $config['owner_id'];
    }
    
    if ($config['count']) {
      return $Attachments->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $Attachments
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $Attachments->_sql();
      return $data ? $data : array();
    }
  }

  function create($data) {
    $Attachments = M('Attachments');
    
    $img_info = getimagesize($data['savepath']);
    if ( $img_info ) {
      $data['is_image'] = 1 ;
      $data['image_info'] = $img_info[3];
      $data['width'] = $img_info[0] ;
      $data['height'] = $img_info[1] ;
    }
    
    $ret = $Attachments->add($data);
    if ($ret === FALSE) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    }
  }
  
  function create_all($data) {
    $Attachments = M('Attachments');
    $ret = $Attachments->addAll($data);
    if ($ret === FALSE) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE );
    } 
  }

  function update($id, $data) {
    $Attachments = M('Attachments');

    $ret = $Attachments->where("id = %d", $id)->save($data);
    if ($ret === FALSE) {
      return ajax_arr('编辑失败', FALSE);
    } else {
      return ajax_arr('编辑成功', TRUE);
    }
  }

  function delete($ids) {
    $Attachments = M('Attachments');
    
    $where = array(
      'id' => array('exp' , " in ( $ids ) ")
    );
    
    $data= $Attachments->where( $where )->select();
    
    foreach ( $data as $row ) {
      if (file_exists($row['savepath']) ) {
        unlink($row['savepath']);
      }
    }

    $ret = $Attachments->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function get_by_type_id($owner_type, $owner_id) {
    $Attach = M('Attachments');

    $data = $Attach->where("ownerType = $owner_type and ownerId = $owner_id")
      ->order('orders ASC , id ASC')
      ->select();

    //echo $Attach->_sql();
    return empty($data) ? array() : $data;
  }

  function create_by_type_id($ownerType, $ownerId, $data) {
    foreach ($data as $key => $val) {
      $data[$key]['ownerType'] = $ownerType;
      $data[$key]['ownerId'] = $ownerId;
      $data[$key]['created'] = date('Y-m-d H:i:s');
      unset($data[$key]['ab_path']);
    }

    $Attach = M('Attachment');

    $ret = $Attach->addAll($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE);
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function delete_by_type_id($ownerType, $ownerId, $delete_file = TRUE) {
    $Attach = M('Attachment');
    $olddata = $this->get_by_type_id($ownerType, $ownerId);


    $ret = $Attach->where('ownerType = %d and ownerId in( %s )', $ownerType, $ownerId)
      ->delete();

    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      if ($delete_file) {
        $this->delete_img($olddata);
      }
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function delete_by_savepath($savepath) {
    $Attach = M('Attachment');
    $ret = $Attach->where('savePath = "%s"', $savepath)->delete();

    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function delete_img($data) {
    foreach ($data as $row) {
      delete_file($row['filePath']);
    }
  }

}
